﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Data;
using Microsoft.Practices.Prism.Commands;
using ParaEditor.Common;
using ParaEditor.DAO.ParaDAO;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Domain.Entities;
using ParaEditor.Infrastructure;
using ParaEditor.DomainSource.Entities;
using System.Collections.Generic;
using ParaEditor.DAO;
using ParaEditor.Desktop.Helper;

namespace ParaEditor.Desktop.ViewModel
{
    [Export(typeof(IPara3007ViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class Para3007ViewModel : ViewModelBase, IPara3007ViewModel
    {
        private Para3007DAO paramDao;
        private SourceBaseDAO sBaseDAO;

        private string lineID;
        public string LineID
        {
            get
            {
                return lineID;
            }
            set
            {
                this.lineID = value;
                this.RaisePropertyChangedEvent("LineID");
            }
        }

        private string lineNmCn;
        public string LineNmCn
        {
            get
            {
                return lineNmCn;
            }
            set
            {
                this.lineNmCn = value;
                this.RaisePropertyChangedEvent("LineNmCn");
            }
        }

        private string lineNmEn;
        public string LineNmEn
        {
            get
            {
                return lineNmEn;
            }
            set
            {
                this.lineNmEn = value;
                this.RaisePropertyChangedEvent("LineNmEn");
            }
        }

        private ICollectionView infoVerList;
        public ICollectionView InfoVerList
        {
            get
            {
                return infoVerList;
            }
            set
            {
                this.infoVerList = value;
                this.RaisePropertyChangedEvent("InfoVerList");
            }
        }

        private ICollectionView paraCollectionDetail;
        public ICollectionView ParaCollectionDetail
        {
            get
            {
                return this.paraCollectionDetail;
            }
            set
            {
                this.paraCollectionDetail = value;
                this.RaisePropertyChangedEvent("ParaCollectionDetail");
            }
        }

        private Visibility editGroupBoxVisible;
        public Visibility EditGroupBoxVisible
        {
            get
            {
                return this.editGroupBoxVisible;
            }
            set
            {
                this.editGroupBoxVisible = value;
                this.RaisePropertyChangedEvent("EditGroupBoxVisible");
            }
        }

        

        //Button
        public DelegateCommand AddParamCommand { get; private set; }
        public DelegateCommand DelParamCommand { get; private set; }
        public DelegateCommand SaveParamCommand { get; private set; }
        public DelegateCommand CanclEditParamCommand { get; private set; }
        public DelegateCommand NewVersionCommand { get; private set; }
        public DelegateCommand CopyCurrentVersionCommand { get; private set; }
        public DelegateCommand ImportLineInfoCommand { get; private set; }

        [ImportingConstructor]
        public Para3007ViewModel()
        {
            sBaseDAO = new SourceBaseDAO();
            paramDao = new Para3007DAO();
            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
            if (paramDao.GetAllVersion().Count == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("当前参数无任何参数版本");
            }
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            EditGroupBoxVisible = Visibility.Hidden;

            //Button
            this.AddParamCommand = new DelegateCommand(this.AddParam);
            this.DelParamCommand = new DelegateCommand(this.DelParam);
            this.SaveParamCommand = new DelegateCommand(this.SaveParam);
            this.CanclEditParamCommand = new DelegateCommand(this.CanclEditParam);
            this.NewVersionCommand = new DelegateCommand(this.NewVersion);
            this.CopyCurrentVersionCommand = new DelegateCommand(this.CopyCurrentVersion);
            this.ImportLineInfoCommand = new DelegateCommand(this.ImportLineInfo);
        }

        private void ImportLineInfo()
        {
            int maxID = paramDao.GetMaxID();
            //从secu查询受益方信息
            IList<CrLineInfo> ownerList = sBaseDAO.GetCurrentLineInfo();
            //转换
            IList<Para3007> lineInfos = DomainConvert.ConvertToPara3007(ownerList, maxID + 1);
            ResultValue resultValue = paramDao.BatchCreateObject(lineInfos);
            if (resultValue.Code == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("导入车站信息成功");
                ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(maxID + 1));
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + resultValue.Msg);
            }
        }

        void InfoVerList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (InfoVerList.CurrentItem != null)
            {
                ReflashDataGrid();
            }
        }

        private void ReflashDataGrid()
        {
            int infoVer = (int)InfoVerList.CurrentItem;
            ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(infoVer));
        }

        private void AddParam()
        {
            if (InfoVerList.CurrentItem == null)
            {
                Microsoft.Windows.Controls.MessageBox.Show("请添加参数版本！才可进行添加");
            }
            else
            {
                EditGroupBoxVisible = Visibility.Visible;
            }
        }

        private void DelParam()
        {
            try
            {
                if (ParaCollectionDetail.CurrentItem != null)
                {
                    Para3007 param = ParaCollectionDetail.CurrentItem as Para3007;
                    if (Microsoft.Windows.Controls.MessageBox.Show("确认删除所选择的参数！", "3007参数", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
                    {
                        //删除
                        ResultValue result = paramDao.DeleteObject(param,param.para3007Pk.InfoVer);
                        if (result.Code == 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除成功");
                            //刷新
                            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;

                            ReflashDataGrid();

                        }
                        else
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除失败!发生错误：" + result.Msg);
                        }
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("未选择要删除的参数");
                }
            }
            catch (Exception e)
            {
            }
        }

        public void SaveParam()
        {
            if (Confirm())
            {
                int point = 0;
                point = paramDao.GetPointByCnName(this.LineNmCn);
                if (point > 0)
                {
                    Para3007 param = new Para3007();
                    Para3007Pk paramPk = new Para3007Pk();
                    paramPk.InfoVer = (int)InfoVerList.CurrentItem;

                    paramPk.LineID = this.LineID;
                    param.para3007Pk = paramPk;
                    param.LineNmCn = this.LineNmCn;
                    param.LineNmEn = this.LineNmEn;
                    param.LineIdbcd = " ";
                    param.LineNmCnPoint = point;
                    ResultValue result = paramDao.CreateObject(param);
                    if (result.Code == 0)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("保存成功");
                        ReflashDataGrid();

                        CanclEditParam();
                    }
                    else
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + result.Msg);
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存失败！未找到此线路名相应的指针，检查此线路名是否正确，或是否存在于3011中!");
                }
            }

        }

        private void CanclEditParam()
        {
            this.EditGroupBoxVisible = Visibility.Hidden;
            this.LineID = null;
            this.LineNmCn = null;
            this.LineNmEn = null;
        }

        private bool Confirm()
        {
            bool flag = true;
            if (this.LineID != null && !"".Equals(this.LineID) && this.LineNmCn != null && !"".Equals(this.LineNmCn) && !"".Equals(this.LineNmEn) && this.LineNmEn != null)
            {
                flag = true;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所填信息不完整");
                flag = false;
            }
            return flag;
        }

        private void NewVersion()
        {
            ObservableCollection<int> infoVerCollection = new ObservableCollection<int>();
            infoVerCollection = paramDao.GetAllVersion();
            int newVersion = paramDao.GetMaxID() + 1;
            infoVerCollection.Add(newVersion);
            InfoVerList = CollectionViewSource.GetDefaultView(infoVerCollection);
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
        }

        private void CopyCurrentVersion()
        {
            //查询当前版本
            ObservableCollection<Para3007> currentVersionParam = new ObservableCollection<Para3007>();
            int infoVer = (int)InfoVerList.CurrentItem;
            currentVersionParam = paramDao.GetObjectByInfoVer(infoVer);

            if (currentVersionParam.Count != 0)
            {
                Para3007 param = null;
                Para3007Pk parampk = null;
                int newVersion = paramDao.GetMaxID() + 1;
                foreach (Para3007 item in currentVersionParam)
                {
                    param = new Para3007();
                    parampk = new Para3007Pk();
                    parampk.InfoVer = newVersion;
                    parampk.LineID = item.para3007Pk.LineID;
                    param.para3007Pk = parampk;
                    param.LineIdbcd = item.LineIdbcd;
                    param.LineNmCn = item.LineNmCn;
                    param.LineNmCnPoint = item.LineNmCnPoint;
                    param.LineNmEn = item.LineNmEn;
                    
                    ResultValue result = paramDao.CreateObject(param);
                    if (result.Code != 0)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("复制过程中出现错误!错误信息：" + result.Msg);
                    }
                }
                //刷新
                ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(newVersion));
                InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所查询的当前版本没有数据,不可进行复制");
            }
        }
    }
}
